package view.subwindosw;

import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import table.GenericTableModel;
import view.main.Main;
import BaseClass.BaseSubFrame;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import model.entity.Company;
import model.initializeEntity.IntCompany;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ListSelectionModel;

public class CompanyFrame extends BaseSubFrame<Company> {
	private JTextField textField;

	public CompanyFrame() throws ClassNotFoundException, SQLException {
		JTableModel = new GenericTableModel<Company>(new Company());
		setLocationRelativeTo(null);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				new Main().setCompany_windows(false);
			}
		});
		setResizable(false);
		setTitle("藥廠公司管理");
		setBounds(100, 100, 655, 421);
		getContentPane().setLayout(null);
		JLabel label = new JLabel("新增藥廠名稱：");
		label.setFont(new Font("新細明體", Font.PLAIN, 20));
		label.setBounds(27, 29, 148, 38);
		getContentPane().add(label);

		textField = new JTextField();
		textField.setBounds(174, 34, 187, 29);
		getContentPane().add(textField);
		textField.setColumns(10);

		JButton button = new JButton("新增");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String comname = textField.getText();
				Company c = new Company();
				c.setComPanyName(comname);
				c.setOid((long) (comVs.size()));
				c.setCreatdate(new Timestamp(new Date().getDay()));
				c.setUpdatedate(new Timestamp(0));
				comVs.addElement(c);
				JTableModel.fireTableDataChanged();
			}
		});
		button.setFont(new Font("新細明體", Font.PLAIN, 20));
		button.setBounds(392, 33, 111, 31);
		getContentPane().add(button);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(27, 80, 608, 267);
		getContentPane().add(scrollPane);

		table = new JTable();
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		table.setFont(new Font("新細明體", Font.PLAIN, 20));
		scrollPane.setViewportView(table);
		table.getAutoResizeMode();
		table.setRowHeight(25);
		table.getTableHeader().setReorderingAllowed(false);
		JButton delBt = new JButton("刪除");
		delBt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JTableModel.removeRow(table.getSelectedRow());
			}
		});
		delBt.setFont(new Font("新細明體", Font.PLAIN, 18));
		delBt.setBounds(524, 33, 111, 31);
		getContentPane().add(delBt);
		setLocationRelativeTo(null);
		Initialize();
	}

	public JTextField getTextField() {
		return textField;
	}

	public void setTextField(JTextField textField) {
		this.textField = textField;
	}

	@Override
	public void Initialize() throws ClassNotFoundException, SQLException {
		IntCompany comps = new IntCompany();
		InitializeTable(this.table, Objects);
		table.setModel(JTableModel);
	}

	public void InitializeTable(JTable table, List<Company> comps) {

		JTableModel.setDataVector(comVs);
	}
}
